<!--
 * @Description: 
 * @Author: shilinzhou
 * @Date: 2024-02-19 15:17:24
 * @LastEditors: James324
 * @LastEditTime: 2024-03-27 10:29:23
-->
<script setup lang="ts">
import { searchConfig } from './config/search-config.ts';
import { tableConfig } from './config/table-config.ts';
import { generateNameToLevel } from '@/config/constant';
import type { MaterialSetting } from '@/api/base/materialsManagement/model/type';
import { doGetMaterialUnit } from '@/api/base/materialsManagement';

const props = defineProps(['visible', 'id']);
const emit = defineEmits(['update:visible', 'ok']);
const dialogVisible = ref(false);
const tableRef = ref<TableRef>();
const searchRef = ref<SearchRef>();
const { handleFullscreen, getMaxHeight } = useModal();

watch(
    () => dialogVisible.value,
    val => {
        emit('update:visible', val);
    }
);

watch(
    () => props.visible,
    val => {
        dialogVisible.value = val;
    }
);

watch(
    () => tableRef.value?.tableData,
    tableData => {
        if (!props.id) return;
        if (tableData && Object.keys(tableData).length) {
            // 初始化生产单位列表选择行
            const row = tableData.filter((item: MaterialSetting) => item.id === props.id)[0];
            if (row) tableRef.value?.setCurrentRow(row); // 设置表格选中当前行
        }
    }
);

/**
 * 生产单位单选
 */
const handleOk = () => {
    const selectRow = tableRef.value?.selectTableRowData;
    dialogVisible.value = false;
    emit('ok', selectRow || {});
};
</script>

<template>
    <q-modal
        v-model:visible="dialogVisible"
        title="选择单位"
        ok-text="保存"
        width="900px"
        :fullscreen="true"
        :centered="true"
        @ok="handleOk"
        @fullscreen="handleFullscreen"
    >
        <base-search ref="searchRef" :table-ref="tableRef" :search-config="searchConfig">
        </base-search>
        <base-table
            :id="generateNameToLevel('2')"
            ref="tableRef"
            :max-height="getMaxHeight"
            :tableConfig="tableConfig"
            :getDataFn="doGetMaterialUnit"
            :modal-mode="true"
            :show-setting="false"
            :un-action="true"
        >
        </base-table>
    </q-modal>
</template>

<style scoped lang="scss"></style>
